<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<?php 
$zone = $_POST['name'];
$desc = $_POST['maincont'];
$authorised = $_POST['authorised'];
	
if(isset($_POST['saveclassifieds']))
{
	
	$news_pic = $_FILES['image']['name'];
				
	if(!empty($news_pic)) {
		$temp_name = str_replace($spl_char, "", $news_pic);
		$temp_name =  preg_replace("([.]+)",".",$temp_name);
		$stored_image_name = mktime(date("H"), date("i"), date("s"), date("n"), date("j"), date("Y"))."_".$temp_name;
		move_uploaded_file($_FILES['image']['tmp_name'], "../uploads/".$stored_image_name);
	}
	
	
	if($_GET['m']!=edit)
	{
	
		mysql_query("INSERT INTO classifieds_category (`category`, `desc`, stored_image_name,`active_status`, `created_datetime`) VALUES ('$zone','$desc','$stored_image_name','$authorised',now())");	
		//header("location: index.php?tc=addcat&_s=1");
		echo "<script>window.location='index.php?tc=classifieds_category&_s=3'</script>";
	}
	
}
if(isset($_POST['updateclassifieds']))

{
	$news_pic = $_FILES['image']['name'];
				
	if(!empty($news_pic)) {
		if (file_exists("../uploads/".$_POST['old_image_name'])) {
			unlink("../uploads/".$_POST['old_image_name']);
		}
		$temp_name = str_replace($spl_char, "", $news_pic);
		$temp_name =  preg_replace("([.]+)",".",$temp_name);
		$stored_image_name = mktime(date("H"), date("i"), date("s"), date("n"), date("j"), date("Y"))."_".$temp_name;
		move_uploaded_file($_FILES['image']['tmp_name'], "../uploads/".$stored_image_name);
	}

mysql_query("UPDATE classifieds_category  SET `category` = '$zone',`desc` ='$desc', 
			stored_image_name='$stored_image_name',`active_status` ='$authorised',
			`modified_datetime`=now() where id='".$_POST['hid']."'")
or 
die(mysql_error());

	
	echo "<script>window.location='index.php?tc=classifieds_category&_s=1'</script>";
	
}
?>

<script type="text/javascript">
function validate()
{
	if(document.up.name.value=="")
	{
		alert("Category Name Missing.");	
		document.up.name.focus();
		return false;
	}
	
	var exp = /^.+\.(jpg|gif|jpeg|JPG|JPEG|GIF|png|PNG)$/;
		if (!exp.test(document.up.image.value))
		{				   
			alert("Please Choose JPG OR GIF File");
			return false;
		}	
}
</script>
<?php if($_REQUEST['_s'] == 1){ ?>
    <div class="sucmsg"><img src="images/information.png" align="absmiddle" />&nbsp;Successfully saved.</div>
<?php } ?>
<?php if($_REQUEST['_e'] == 1){ ?>
    <div class="sucmsg"><img src="images/error.png" align="absmiddle" />&nbsp;</div>
<?php } ?>
<?php 
$ssql="select * from classifieds_category where id='".$_GET['_d']. "'";
$ssql=mysql_query($ssql);
$row =mysql_fetch_array($ssql);
if($row['active_status']=='1')
{
	
$chkact = 'checked="checked"';
}
if($row['active_status']=='0')
{
	
$chkdec = 'checked="checked"';
}
?>
<form name="up" action="#" method="post" enctype="multipart/form-data" onSubmit="return validate()">
<table width="99%" align="center" border="0"  class="list" cellpadding="5">
<input type="hidden" name="hid" value="<?php echo  $row['id'];?>" />
<thead>
<tr>
	<td colspan="2" align="left">
        <div style="float:left" class="righthead">Classifieds Category</div>
        <div style="float:right"><a href="index.php?tc=classifieds_category" class="back">View Category</a></div>
    </td>
</tr>
</thead>
<tr>
	<td align="right" width="150">Category Name *</td>
    <td align="left"><input type="text" name="name" style="width:200px;" value="<?php echo  $row['category'];?>"  /></td>
</tr>
<tr>
	<td align="right" width="250" valign="top">Category Description</td>
    <td align="left">
    <textarea name="maincont" id="maincont" class="textarea" cols="45" rows="8"><?php echo  $row['desc'];?></textarea>
        <script type="text/javascript">CKEDITOR.replace( 'maincont',{skin : 'kama'});</script>
   </td>
</tr>
<tr>
	<td align="right" width="250">Upload Image *</td>
    <td align="left"><input type="file" name="image" style="width:200px;" value="<?php echo  $row['stored_image_name'];?>"  /></td>
</tr>
<tr>
  <td align="right" width="150">Status :</td>
  <td align="left">
  <input name="authorised" type="radio"  checked="checked" value="1" <?php echo $chkact;?> />&nbsp;Active&nbsp;&nbsp;
  <input  name="authorised" type="radio" value="0" <?php echo $chkdec;?> />&nbsp;Deactive</td>
</tr>

<tr>
	<td colspan="2" style="padding-left:155px;">
    	<input type="hidden" name="old_image_name" id="old_image_name" value="<?php echo $row['stored_image_name'];?>" />
    	<?php if($_GET['m']!=edit){?>
        <input type="submit" value="Save" name="saveclassifieds" >&nbsp;&nbsp;
        <?php }
        else
		{
         ?>
          <input type="submit" value="Update" name="updateclassifieds" >
         <?php 
		
		}
         ?>
    </td>
</tr>        
</table>   
</form> 
